package com.meatball.workflow.service.impl

import com.meatball.workflow.service.WorkflowService
import okio.IOException
import org.activiti.engine.RepositoryService
import org.activiti.engine.repository.Deployment
import org.apache.commons.io.FilenameUtils
import org.springframework.stereotype.Service
import org.springframework.web.multipart.MultipartFile
import java.util.zip.ZipInputStream

/**
 * TODO 工作流服务实现层
 *
 * @author 张翔宇
 * @since 2024年3月15日
 */
@Service
class WorkflowServiceImpl (private val repositoryService: RepositoryService) : WorkflowService {
    override fun uploadFileAndDeployment(processFile: MultipartFile, processName: String?) : Deployment {
        var processNameVar = processName
        val originalFilename = processFile.originalFilename
        val extension = FilenameUtils.getExtension(originalFilename)
        if (processName == null) {
            processNameVar = originalFilename
        }
        val inputStream = processFile.inputStream
        val deployment: Deployment = when (extension) {
            "zip" -> {
                // 压缩包部署方式
                val zipInputStream = ZipInputStream(inputStream)
                repositoryService.createDeployment().addZipInputStream(zipInputStream).name(processNameVar).deploy()
            }
            "bpmn" -> {
                // bpmn文件部署方式
                repositoryService.createDeployment().addInputStream(originalFilename, inputStream).name(processNameVar).deploy()
            }
            else -> throw IllegalArgumentException("部署失败。")
        }
        return deployment
    }
}